package servlet;

import entity.Page;
import entity.PersonalFile;
import service.PersonalFileService;
import service.PersonalFileServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.print.Book;
import java.io.IOException;
import java.util.List;

/**
 * @author 孙亚梅
 * @version V1.0
 * @Project javase2203
 * @Package servlet
 * @Description:
 * @Date 2022/5/27 14:55
 */
@WebServlet("/PersonalFileList" )
public class PersonalFileServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");

       PersonalFileService service = new PersonalFileServiceImpl();


        //获取当前需要跳转到的页数
        String pageIndex = request.getParameter("pageIndex");
        System.out.println(pageIndex);
        //如果当前页等于null，那么让他跳转到第一页
        if (pageIndex == null){
            pageIndex="1";
        }
        //封装page分页对象，第一个参数：当前需要跳转到的页码，第二个参数：每页显示条数
        Page page = new Page(Integer.parseInt(pageIndex), 5);


        //分页查询，查询结果集
        List<PersonalFile> personalFileList=service.findAllPage(page);

        //分页查询，查询中记录数
        int count = service.finAllPageCount();
        page.setTotalCounts(count);


        //使用jsp来展示书籍列表
        //如果重定向到jsp页面，需要将数据保存到session
        //如果使用请求转发，可以将数据保存到request
        request.setAttribute("PersonalFileList", personalFileList);
        //将分页对象传给页面展示
        request.setAttribute("page", page);
        //将当前页返回给页面
        request.setAttribute("pageIndex", pageIndex);
        request.getRequestDispatcher("/PersonalFileList.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}






